package com.calvin.study.event.listener;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.calvin.study.event.base.BaseEvent;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class BaseEventListener {

	// 将所有实现类（AbstractEventExecutor）注入到该map中
	// 使用Spring依赖注入的特性，完成属性注入（注入继承AbstractEventExecutor该类的所有子类）。
	@Autowired
	private Map<String, AbstractEventExecutor> executorMap;

	@Async
	@EventListener
	public void onHandlerEvent(BaseEvent event) {
		log.info("[BaseEventListener][onHandlerEvent]，eventSource=" + event.getSource().getClass().getSimpleName()
				+ "，eventContent=" + event.getEventContent() + "，eventExecutor=" + event.getEventExecutor());
		executorMap.get(event.getEventExecutor()).execute(event.getEventContent());
	}

}
